<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Live extends MY_Controller{

	public static  $m_Type = array(
			DOUBAN_TYPE,
			);
	
	public static $m_next = array(
			1 => 'book',
			2 => 'movie',
			);
	
	public static $m_baseID = 1000000;
	
	public static $m_deep = 100; // 假设一百张A4纸张 是1厘米厚
	
	
	
	public function __construct()
	{
		parent::__construct();
	
		$this->load->library('my_api',self::$m_Type);
		
		$this->load->model('date_m');
		$this->load->model('douban/douban_book_m');
	
	}
	
	public function index()
	{
		$this->m_arrSub = array(
				'common/header'  =>  'index',
				'common/footer'  => 'index',
				);
		$this->addStyle('public/css/douban/live.css');
		
		$this->getSub();
		
		
		// 获取豆瓣的个人信息..
		
		$data = array(
			'id' => $this->my_api->douban_uid,
		);
		
		$doubanUserInfo  = $this->my_api->douban->api('User.info.GET',$data)->makeRequest();
		
		
		// 获取注册年份的大事件
		
		$year = substr($doubanUserInfo['created'],0,4);
		
		$dateList = $this->date_m->getDateList(array('date' => $year));
		
		$this->data['dateList'] = array();
		
		if (is_array($dateList) && !empty($dateList))
		{
			foreach ($dateList as $date) 
			{
				$this->data['dateList'][] = array(
						'id' => $date['id'],
						'desp'=> $date['desp'],
						'imgUrl'=>$date['imgUrl'],
						);
			}
		}
		
		$this->data['userInfo'] = array(
				'name' =>$doubanUserInfo['name'],
				'city'=>$doubanUserInfo['loc_name'],
				'avatar' =>"http://img3.douban.com/icon/ul".$doubanUserInfo['id']."-22.jpg",
				'rank' =>$doubanUserInfo['id'] -  self::$m_baseID,
				'time' => $doubanUserInfo['created'],
				);
		
		
		// 获取注册时间的小时 
		$createHour = substr($doubanUserInfo['created'], 11,2);
		
		$createHourStr = null;
		if ($createHour > 1 && $createHour < 6)
		{
			$createHourStr = '夜深人静的时刻';
		}
		elseif ( 6 <$createHour && $createHour< 9)
		{
			$createHourStr = "迷人的早晨";
		}
		elseif ( 9 < $createHour &&  $createHour< 12)
		{
			$createHourStr = "上午";
		}
		elseif (12 < $createHour &&  $createHour< 14)
		{
			$createHourStr ="你没有在中午稍作休憩";
		}
		elseif (14 < $createHourStr &&  $createHour< 17)
		{
			$createHourStr = "下午";
		}
		elseif (17 < $createHour &&  $createHour< 19)
		{
			$createHourStr = "傍晚";
		}
		elseif (19 < $createHour &&  $createHour< 22)
		{
			$createHourStr ="晚上正式你精力旺盛的时刻";
		}
		else 
		{
			$createHourStr = '深夜了你还未有睡意..';
		}
		
		$this->data['userInfo']['createHourStr'] = $createHourStr;
		
		
		
		$this->data['ajaxUrl'] = array(
				'next' => "index.php/douban/live/next",
				);

		
		$this->load->view('douban/live/user',$this->data);
		
	}
	
	public function book()
	{
		// 获取个人用户个人信息
		$data = array(
				'id' => $this->my_api->douban_uid,
		);
		
		$doubanUserInfo  = $this->my_api->douban->api('User.info.GET',$data)->makeRequest();
		
		
		// 获取用户的所有读书信息  最大值为100
		
		$bigReadBookList = array();
		$bookPageTotal   = 0;
		$bookRateTotal   = 0;
		$bookRateNums    = 0;
		$data = array(
				'name' => $doubanUserInfo['uid'],
				'status' =>'read',
				'start' => 0,
				'count' => 100,
		);
		
		$bookList  = $this->my_api->douban->api('Book.collections.GET',$data)->makeRequest();
		
		$bigReadBookList['total'] = $bookList['total'];
		
		foreach ($bookList['collections'] as $book)
		{
			$bigReadBookList['list'][] = $book;
			
			$bookPageTotal += $book['book']['pages'];
			if (isset($book['rating']))
			{
				$bookRateTotal += $book['rating']['value'];
				
				if ($book['rating']['value'] > 0 && $book['rating']['value'] <6)
				{
					$bookRateNums ++;
				}
			}
			
		}
		
		$page = 1;
		if ($bigReadBookList['total'] > 100)
		{
			$page = ((int)$bigReadBookList['total']/100)+1;
		}
		
		for ($i = 1;$i <$page;$i++)
		{
			$data = array(
					'name' => $doubanUserInfo['uid'],
					'status' =>'read',
					'start' => $i*100+1,
					'count' => 100,
			);
			
			$bookList  = $this->my_api->douban->api('Book.collections.GET',$data)->makeRequest();
			
			foreach ($bookList['collections'] as $book)
			{
				$bigReadBookList['list'][] = $book;
				$bookPageTotal += $book['book']['pages'];
				
				if (isset($book['rating']))
				{
					$bookRateTotal += $book['rating']['value'];
					
					if ($book['rating']['value'] > 0 && $book['rating']['value'] <6)
					{
						$bookRateNums ++;
					}
				}
			}
		}
	
		$data = array(
				'name' => $doubanUserInfo['uid'],
				'start' => 0,
				'count' => 1000,
		);
		
		$bigTagList  = $this->my_api->douban->api('Book.userTags.GET',$data)->makeRequest();
		
		
		$this->data['book'] = array(
				'list' =>$bigReadBookList['list'],
				'totalPage' => $bookPageTotal,
				'totalRateNums' => $bookRateNums,
				'avRate' => $bookRateTotal/$bookRateNums,
				'total' => $bigReadBookList['total'],
				);
		
		// 写入mongodb 
		//$this->douban_book_m->addData($this->my_api->douban_uid,$this->data['book']);
		
		$isExist = $this->douban_book_m->isUserExist($this->my_api->douban_uid);
		
	     if ($isExist === false)
	     {
	     	$this->douban_book_m->addData($this->my_api->douban_uid,$this->data['book']);
	     }
		
		$this->load->view('douban/live/book',$this->data);
	}
	
	public function next()
	{
		$arrRet = array();
		$id = $this->input->post('nextid',true);
		
		
		if (isset(self::$m_next[$id + 1]))
		{
			$arrRet['status'] = 1;
			$arrRet['nextid'] = $id +1;
			$arrRet['next'] = 'index.php/douban/live/'.self::$m_next[$id+1];	
			$arrRet['nextclass'] ="douban_".self::$m_next[$id+1];
		}
		else
		{
			$arrRet['status'] = 0;
		}
		
		echo json_encode($arrRet);
	}
	
}
